package cn.sofwin.sb;

import cn.sofwin.sb.Service.UserService;
import cn.sofwin.sb.core.annotaion.MyDao;
import cn.sofwin.sb.dao.UserDao;

import java.lang.reflect.Field;

public class Main {
    public static void main(String[] args) {
        /*UserService userService = new UserService();
        initAutowired(userService);
        System.out.println("userDao:" + userService.getUserDao());*/
    }

    public static  void initAutowired(Object obj){
        Field[] fields = obj.getClass().getDeclaredFields();
        try {
            for (Field field :fields){
                    MyDao mydao = field.getAnnotation(MyDao.class);
                    if(mydao != null){
                        System.out.println("发现友军" + field.getName());
                        field.setAccessible(true);
                        //第一阶段，实现设置属性值的操作
                        //field.set(obj,new UserDao());

                        //第二阶段，动态设置对应数据类型的对象
                        System.out.println(field.getGenericType());
                        field.set(obj,field.getType().newInstance());
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
